﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace UI.Admin
{
    public partial class khachhang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }
        void HienThiDanhSach()
        {
            // Get All Sản Phẩm
            DataTable Manufacturers = DBConnection.getALL("sp_getAllKhachHang");
            GridView1.DataSource = Manufacturers;
            GridView1.DataBind();

            MultiView1.ActiveViewIndex = 0;
            btninsert.Enabled = false;
        }


        void HienThiKetQuaSua(String manxb)
        {

            SqlParameter[] thamso = new SqlParameter[]
            {
                 new SqlParameter("@MaKH",manxb)
            };
            DataTable dtsp = DBConnection.GetDatatableByStoreProcedureName("sp_getKhachHangbyMaKH", thamso);
            lbmakh.Text = dtsp.Rows[0]["MaKH"].ToString();
            txtuname.Text = dtsp.Rows[0]["UserName"].ToString();
            txtpass.Text = dtsp.Rows[0]["Password"].ToString();
            txtemail.Text = dtsp.Rows[0]["Email"].ToString();
            txtcmnd.Text = dtsp.Rows[0]["CMND"].ToString();
            txthoten.Text = dtsp.Rows[0]["HoTen"].ToString();
            txtngaysinh.Text = dtsp.Rows[0]["NgaySinh"].ToString();
            txtgioitinh.Text = dtsp.Rows[0]["GioiTinh"].ToString();
            txtSDT.Text = dtsp.Rows[0]["SDT"].ToString();
            txtdiachi.Text = dtsp.Rows[0]["DiaChi"].ToString();
            txtngaydangky.Text = dtsp.Rows[0]["NgayDangKy"].ToString();
            txtrand.Text = dtsp.Rows[0]["Rand"].ToString();
            txttongtien.Text = dtsp.Rows[0]["TongTien"].ToString();
            if (dtsp.Rows[0]["IsActive"].ToString() == "True")
            {
                chkactive.Checked = true;
            }
            else
                chkactive.Checked = false;

            Image2.ImageUrl = "~/images/" + dtsp.Rows[0]["Avatar"].ToString();
            MultiView1.ActiveViewIndex = 1;
        }

        protected void btntaomoi_Click(object sender, EventArgs e)
        {
            //btnupdate.Visible = false;
            //btninsert.Visible = true;
            //MultiView1.ActiveViewIndex = 1;
            //btninsert.Enabled = true;
            //lbmaNSX.Text = "Mã tự động tăng";

        }


        protected void btntmkiem_Click(object sender, EventArgs e)
        {

            //string tennxb = txttim.Text;
            //SqlParameter[] thamso = new SqlParameter[]{
            //new SqlParameter("p_tennhaxb",tennxb)
            //};

            //// search NhaXB by TenNhaXB
            //DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchNhaXuatBanByTenNhaXB", thamso);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //txttim.Text = "";
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.HienThiDanhSach();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;

            string masp = GridView1.SelectedRow.Cells[2].Text;
            this.HienThiKetQuaSua(masp);
        }
        protected void btnback_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }


        protected void btnupdate_Click(object sender, EventArgs e)
        {
            //SqlParameter[] thamso = new SqlParameter[] {
            //new SqlParameter("@MaNSX", int.Parse(lbmaNSX.Text)),
            //};
            //DataTable dt = DBConnection.GetDatatableByStoreProcedureName("sp_getNhaSXbyMaNSX", thamso);
            //string hinh;
            //if (FileUpload2.HasFile)
            //{
            //    string strfile = "~/images/" + FileUpload2.FileName;
            //    string s = Server.MapPath(strfile);
            //    FileUpload2.SaveAs(s);
            //    hinh = FileUpload2.FileName;
            //}
            //else
            //    hinh = dt.Rows[0]["Image1"].ToString();




            //SqlParameter[] thamso1 = new SqlParameter[]
            //{
            //new SqlParameter("@MaNSX",int.Parse(lbmaNSX.Text)),
            //new SqlParameter("@TenNSX",txttenNSX.Text),
            //new SqlParameter("@TrangChu",txttrangchu.Text),
            //new SqlParameter("@LinkImage",hinh),
            // };

            //DBConnection.QueryByStoreProcedureName("sp_updateNhaSX", thamso1);
            //this.HienThiDanhSach();
        }


        protected void btninsert_Click(object sender, EventArgs e)
        {

            ////tao ma nha xuat ban tu dong tang = lay max manhaxb+1 
            //DataTable dt = DBConnection.getALL("cuontNhaSX");
            //int maSP = int.Parse(dt.Rows[0]["soluong"].ToString()) + 2;

            //string tenNSX = txttenNSX.Text;
            //string hinh = FileUpload2.FileName;
            //SqlParameter[] thamso = new SqlParameter[]
            //{
            //new SqlParameter("@MaNSX",maSP),
            //new SqlParameter("@TenNSX",tenNSX),    
            //new SqlParameter("@TrangChu",txttrangchu.Text),
            //new SqlParameter("@LinkImage",hinh),
            // };
            //DBConnection.QueryByStoreProcedureName("insetNhaSX", thamso);
            //this.HienThiDanhSach();
        }


        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            //string maNSX = GridView1.Rows[e.RowIndex].Cells[2].Text;

            //try
            //{
            //    SqlParameter[] thamso = new SqlParameter[]
            //     {
            //    new SqlParameter("@MaNSX",int.Parse(maNSX)),
            //     };
            //    DBConnection.QueryByStoreProcedureName("sp_deleteNhaSXbyMaNSX", thamso);
            //}
            //catch
            //{
            //    System.Console.WriteLine("khong xoa duoc do rang buoc khoa");
            //}
            //HienThiDanhSach();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
}